//
//  QShowWebView.m
//  QoilPublic
//
//  Created by 杨凯 on 16/11/25.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QShowWebView.h"
#import "QShowSubWebView.h"
@implementation QShowWebView{

    UIControl *_maskView;
    QShowSubWebView *_subView;
    
    CGFloat W;
    CGFloat H;

}

- (instancetype)initWithUrl:(NSString *)url{
    
    self = [super init];
    if(self){
        W = 280;
        H = 350;
        self.frame = [UIScreen mainScreen].bounds;
        _urls = url;
        [self createUI];
    }
    return self;
}

- (void)createUI{
    __block QShowWebView *weakSelf = self;
    _maskView = [[UIControl alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _maskView.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.5];
    _maskView.hidden = YES;
    [self addSubview:_maskView];
    
    _subView = [[[NSBundle mainBundle] loadNibNamed:@"QShowSubWebView" owner:self options:nil] lastObject];
    _subView.url = _urls;
    _subView.layer.cornerRadius = 4.0;
    _subView.frame = CGRectMake((kScreenWidth-W)/2, kScreenHeight, W,H);
    [_maskView addSubview:_subView];
    _subView.block = ^(){
        [weakSelf hidePlayBoxView];
        if(weakSelf.block){
            weakSelf.block();
        }
    };

}

- (void)showPlayBoxView{
    
    UIWindow *window = [[UIApplication sharedApplication].delegate window];
    [window addSubview:_maskView];
    _maskView.hidden = NO;
    _maskView.alpha = 0;
//    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        _subView.center = self.center;
        _maskView.alpha = 1;
//    } completion:^(BOOL finished) {
//        
//    }];
}

- (void)hidePlayBoxView{
    _maskView.alpha = 1;
    [UIView animateWithDuration:.3 animations:^{
        _subView.frame = CGRectMake((kScreenWidth-W)/2, kScreenHeight,W,H);
        _maskView.alpha = 0;
    } completion:^(BOOL finished) {
        _maskView.hidden = YES;
        [_maskView removeFromSuperview];
        [_subView removeFromSuperview];
        [self removeFromSuperview];
    }];
}


@end
